﻿using CRM.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CRM.Controllers.Customer
{
    using CustomerModel = CRM.Models.Customer;

    [Route("api/[controller]")]
    [ApiController]
    public class CustomerHomeController : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly UserManager<User> _userManager;
        public CustomerHomeController(MyDbContext dbContext, UserManager<User> userManager)
        {
            _dbContext = dbContext;
            _userManager = userManager;
        }

        [HttpGet(nameof(Workbench))]
        public async Task<ResultStruct> Workbench()
        {
            var user = await _userManager.GetUserAsync(User);
            var Model = _dbContext.Customer as IQueryable<CustomerModel>;

            Model = Model.Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.UserId == user.Id);

            var List = await Model
               .OrderByDescending(m => m.Id)
               .ToListAsync();
            //客户总数
            var CustomerCount = List.Count;
            //成交客户
            var DealCustomerCount = List.Where(m => m.DealStatus == 1).Count();

            //7天内移入公海
            var WaterCustomerList = List.Where(m => m.IsPrivate == 0).ToList();
            var SevenDaysAgo = DateTime.Now.AddDays(-7);

            List<CustomerModel> WeekWaterList = new() { };

            foreach (var item in WaterCustomerList)
            {
                if (item.UpdatedAt > SevenDaysAgo)
                {
                    WeekWaterList.Append(item);
                }
            }

            var WaterCustomerCount = WeekWaterList.Count;

            //七天未跟进
            var SevenDaysNotFollow = List.Where(m => m.FollowAt < SevenDaysAgo).Count();

            //15天未跟进
            var FifteenDaysAgo = DateTime.Now.AddDays(-15);
            var FifteenDaysNotFollow = List.Where(m => m.FollowAt < FifteenDaysAgo).Count();

            //一个月未跟进
            var OneMonthAgo = DateTime.Now.AddMonths(-1);
            var OneMontNotFollow = List.Where(m => m.FollowAt < OneMonthAgo).Count();

            //一年未跟进
            var OneYearAgo = DateTime.Now.AddYears(-1);
            var OneYearNotFollow = List.Where(m => m.FollowAt < OneYearAgo).Count();

            //更久未跟进
            var LongTimeNotFollow = CustomerCount - OneYearNotFollow;

            //已发营销邮件
            var EmailList = await _dbContext.EmailSendHistory
                .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.UserId == user.Id)
                .ToListAsync();
            var EmailCount = EmailList.Count;

            return Success(new
            {
                CustomerCount = CustomerCount,
                DealCustomerCount = DealCustomerCount,
                WaterCustomerCount = WaterCustomerCount,
                SevenDaysNotFollow = SevenDaysNotFollow,
                FifteenDaysNotFollow = FifteenDaysNotFollow,
                OneMontNotFollow = OneMontNotFollow,
                OneYearNotFollow = OneYearNotFollow,
                LongTimeNotFollow = LongTimeNotFollow,
                EmailCount = EmailCount
            });

        }
    }
}
